﻿#region Librerias

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESF.General;
using System.IO;
using ESF.FileProcessing;
using ESF.WinForms;

#endregion


namespace ESF.FileHelper.Controls
{
	public partial class uscCopyDirectories : UserControl
	{
		#region Eventos

		public event Program.DelegateReturnMenu ReturningMenu;

		#endregion
		

		#region Members

		private string _TempLogFileName;

		#endregion


		#region Constructor
		
		public uscCopyDirectories()
		{
			_TempLogFileName = Path.GetTempFileName () + ".txt";

			InitializeComponent();

			ListLoader.LoadList (ddlRenameAction, 
				"Copie renombrado", "JustCopyRenamed",
				"Solo si existe renombre", "OnlyIfExistsRename", 
				"Renombre y sobreescriba", "RenameAndOverwrite",
				"No renombre solo sobreescriba", "DontRenameOverwrite",
				"Omita si existe", "DoNothingIfExists");

			ListLoader.RemoveSelectItem (ddlRenameAction);
		}

		#endregion


		#region Methods

		private string ChooseFolder ()
		{
			if (fbdSelectFolder.ShowDialog(this) == DialogResult.OK)
			{
				return fbdSelectFolder.SelectedPath;
			}

			return string.Empty;
		}

		#endregion


		#region Button Events

		private void btnReturnMenu_Click(object sender, EventArgs e)
		{
			if (ReturningMenu != null)
			{
				ReturningMenu();
			}
		}


		private void btnCopyDirectories_Click(object sender, EventArgs e)
		{
			TextWriter logWriter;


			lnkOpenCopyLog.Visible = false;

			try
			{
				logWriter = File.AppendText (_TempLogFileName);

				ProcessFileHelper.CopyDirectories (
					txtOriginFolder.Text, txtDestinationFolder.Text, 
					(ProcessFileHelper.RenameActions) Enum.Parse (typeof (ProcessFileHelper.RenameActions), ListLoader.GetSelectedValueBasicReference (ddlRenameAction).Code),
					txtRenameFormat.Text,
					logWriter);

				lnkOpenCopyLog.Visible = true;

				WinFormsHelper.ShowInfo (this, "La Copia y Renombrado ha sido completado");
			}
			catch (Exception pEx)
			{
				lnkOpenCopyLog.Visible = true; 
				
				WinFormsHelper.ShowError(this, pEx);
			}
		}

		#endregion


		#region Link Events

		private void lnkOpenCopyLog_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			ProcessHelper.StartProcess (_TempLogFileName);
		}

		private void lnkChooseOriginFolder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			txtOriginFolder.Text = ChooseFolder ();
		}

		private void lnkChooseDestinationFolder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			txtDestinationFolder.Text = ChooseFolder();
		}

		#endregion
	}
}
